﻿#include "AbnormalManageWidget.h"
#include "ui_AbnormalManageWidget.h"
#include "CommonFunc.h"
#include "DataInfo.h"

AbnormalManageWidget::AbnormalManageWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AbnormalManageWidget)
{
    ui->setupUi(this);

    appendTypeToFunc(XShowAbnormal, std::bind(&AbnormalManageWidget::showAbnormalInfo, this));
}

AbnormalManageWidget::~AbnormalManageWidget()
{
    delete ui;
}

void AbnormalManageWidget::showAbnormalInfo()
{
    if (g_iProductLineID == -1 || g_iGateWayID == -1)
    {
//        clearAbnormalData();
        g_mapGroupIdToListSensor.clear();
        ui->stopWidget->clearAllData();
        ui->slowWidget->clearAllData();
        return;
    }

    for (int i = 0; i < g_vecProductInfo.size(); ++i)
    {
        const ProductInfo& pi = g_vecProductInfo[i];
        if (pi.id == g_iProductLineID)
        {
            for (int j = 0; j < g_vecGateWay.size(); ++j)
            {
                const GateWay& gw = g_vecGateWay[j];
                if (gw.id == g_iGateWayID)
                {
                    QString title = QString("%1-%2").arg(pi.strName).arg(gw.strName);
                    ui->toolButton_title->setText(title);

                    QVector<QString> vecSlow, vecStop;
                    int iStop{0}, iSlow{0};
                    QMapIterator<int, QList<Sensor> > iter(g_mapGroupIdToListSensor);
                    while (iter.hasNext()) {
                        iter.next();
                        int gID = iter.key();

                        for (int i = 0; i < iter.value().size(); ++i)
                        {
                            const Sensor& ss = iter.value()[i];
                            QString strName = QString("%1%2").arg(gID).arg(ss.cell_code);
                            if (ss.status == "stop")
                            {
                                ++iStop;
                                vecStop.push_back(strName);
                            }
                            else if (ss.status == "slow")
                            {
                                ++iSlow;
                                vecSlow.push_back(strName);
                            }
                        }
                    }

                    title = QString("%1%2").arg(QStringLiteral("停转")).arg(iStop);
                    ui->toolButton_titleStop->setText(title);

                    title = QString("%1%2").arg(QStringLiteral("低速")).arg(iSlow);
                    ui->toolButton_titleSlow->setText(title);

                    ui->slowWidget->addToolButton(vecSlow, "toolButton_slow");
                    ui->stopWidget->addToolButton(vecStop, "toolButton_stop");
                    break;
                }
            }
            break;
        }
    }
}

void AbnormalManageWidget::clearAbnormalData()
{
    ui->toolButton_title->setText(QStringLiteral("产线-网关"));
    ui->toolButton_titleStop->setText(QStringLiteral("停转数量"));
    ui->toolButton_titleSlow->setText(QStringLiteral("低速数量"));
    ui->stopWidget->clearAllData();
    ui->slowWidget->clearAllData();
}

void AbnormalManageWidget::on_pushButton_returnUp_clicked()
{
    executeFuncFromID(XUI_DeviceManage);
}

void AbnormalManageWidget::on_pushButton_returnTop_clicked()
{
    executeFuncFromID(XUI_Login);
}
